package com.woohua.thread.demo;

import java.util.Vector;

/***
 * @title Basket
 * @description：原子性例子：原子操作，线程安全
 * @author woo hua
 * @version 1.0.0
 * @create 2023/3/9 11:13
 **/
public class Basket {

   Vector v = new Vector<Fruit>();

    /**
     * 原子操作可不加同步关键字
     * @param fruit
     */
   public void put(Fruit fruit) {
       v.add(fruit);
   }

    /**
     * 加上 synchronized 关键字
     * @return
     * @throws Exception
     */
   public synchronized Fruit tackOut() throws Exception {
       if(v.size() > 0) {
           return (Fruit) v.remove(0);
       }else {
           throw new Exception("There is no fruit in basket.");
       }
   }
}
